<!-- 动态树形菜单页面 -->
<template>
  <div class="common_screen">

    <!-- 布局容器 -->
    <el-container :style="containerStyle">
      <!-- 头部 -->
      <el-header>

        <el-row>
          <el-col :span="12">
            <div>
              <p>后台管理系统</p>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="logout">
              <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                  {{userName}}<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="userInfo">个人信息</el-dropdown-item>
                  <el-dropdown-item command="updatePwd">修改密码</el-dropdown-item>
                  <el-dropdown-item command="logout">注销登录</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </el-col>
        </el-row>
      </el-header>

      <!-- 中间主体 -->
      <el-container>
        <!-- 左边导航栏 -->
        <el-aside width="200px">

          <el-menu
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
            :default-active="'index'"> <!-- router属性：表示使用router自由切换 -->
            <!-- 动态配置菜单-->
            <template v-for="item in menuList">
              <!-- 是否父级权限判断 -->
              <el-submenu :index=item.index v-if="!item.isContent">
                <template slot="title">
                  <i :class=item.icon></i>
                  {{item.name}}
                </template>
                <tree_menu v-bind:menuData=item.children></tree_menu>
              </el-submenu>

              <el-menu-item :index=item.index v-else>
                <i :class=item.icon></i>
                <span slot="title">{{item.name}}</span>
              </el-menu-item>

            </template>

          </el-menu>
        </el-aside>
        <!-- 右边主界面 -->
        <el-main>
          <router-view/>
        </el-main>
      </el-container>


    </el-container>

  </div>
</template>


<script>

  //使用import导入tree_menu子组件
  import tree_menu from '@/components/common/tree_menu.vue'
  import api from '@/assets/js/main/top_tree.js'
  import axios from 'axios'
  import common from "@/assets/js/common/common.js"


  export default {
    //数据类型
    data() {
      return {
        //布局容器css样式配置
        containerStyle: {
          height: '100%',
          width: '100%'
        },
        userName: "张三",
        //菜单动态数据
        menuList: []
      }
    },
    //在components中引入子组件
    components: {
      tree_menu: tree_menu
    },
    //初始化加载
    mounted: function () {
      this.getMenu();//根据不同的用户获取对应的菜单信息
      this.getUserInfo();
    },
    //具体js方法
    methods: {
      handleOpen(key, keyPath) {
        console.log(key, keyPath)
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath)
      },
      //下来信息点击事件
      handleCommand(command) {
        switch (command) {
          case 'userInfo':
            this.openUserInfo();
            break;
          case 'updatePwd':
            this.openUpdatePwd();
            break;
          case 'logout':
            this.logout();
            break;
        }
      },
      //获取权限菜单
      getMenu() {
        api.getMenu({}).then((res) => {
          this.menuList = res.data;
        });
      },
      //注销登录
      logout() {
        let _this = this;
        axios.get("/api/login/logout", {}).then((res) => {
          if (res.code === 200) {
            _this.$router.push({name: 'login'})//注销登陆
          } else {
            common.generalMessage(res);
          }
        });
      },
      //打开个人信息窗口
      openUserInfo() {
        this.$message('打开个人信息窗口');
      },
      //打开修改密码窗口
      openUpdatePwd() {
        this.$message('打开修改密码窗口');
      },
      //获取个人信息
      getUserInfo() {
        let _this = this;
        axios.get("/api/admin/main/getUserInfo", {}).then((res) => {
          this.userName = res.data.sysUserName;
        });
      }
    }
  }
</script>


<style scoped>


  .el-header {
    background-color: #545c64;
    color: #333;
    line-height: 60px;
  }

  .el-header p {
    /*字体形状*/
    font-family: "微软雅黑";
    font-size: 22px;
    /*位置*/
    text-align: left;
    /*外边距*/
    margin: 0 20px 0 0;
    /*字体颜色*/
    color: #FFF;
  }

  .el-aside {
    background-color: #545c64;
  }

  .logout {
    /*位置*/
    text-align: right;
    /*外边距*/
    margin: 0 0 0 100px;
  }

  .logout span {
    color: #FFF;
    font-size: 15px;
  }

  .logout a {
    color: #FFF;
    font-size: 15px;
    /*取消a标签的下划线*/
    text-decoration: none;
  }

  .logout a span {
    color: #FFF;
    font-size: 15px;
    margin-left: 5px;
    /*取消a标签的下划线*/
    text-decoration: none;
  }

</style>
